package com.sas.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

import org.hibernate.annotations.NaturalId;
import org.springframework.util.Assert;

@Entity
public class ArticleField extends PersistentEntity {
	
	@Column(nullable = false)
	@NaturalId
	private String name;
	private String description;
	
	@ManyToMany(cascade = CascadeType.ALL, mappedBy = "fields")
	private Set<Article> articles;
	
	@OneToMany(cascade = CascadeType.ALL)
	private Set<ArticleField> subArticleFields;
	
	private boolean root = false;
	
	ArticleField() {
	}
	
	public ArticleField (String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		Assert.hasText(name);
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		Assert.notNull(description);
		this.description = description;
	}

	public Set<Article> getArticles() {
		return articles;
	}

	public void setArticles(Set<Article> articles) {
		Assert.notNull(articles);
		this.articles = articles;
	}

	public Set<ArticleField> getSubArticleFields() {
		return subArticleFields;
	}

	public void setSubArticleFields(Set<ArticleField> subArticleFields) {
		Assert.notNull(subArticleFields);
		this.subArticleFields = subArticleFields;
	}

	public void setRoot(boolean root) {
		this.root = root;
	}

	public boolean isRoot() {
		return root;
	}
	
	@Override
	public String toString() {
		return this.name;
	}
	
	/**
	 * set a given article that belongs to this article field.
	 */
	public void addArticle(Article article) {
		if (articles == null) {
			articles = new HashSet<Article>();
		}
		
		articles.add(article);
	}
	
	
}
